package entities.box_plot;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import data.enums.ArchiveDescription;
import data.enums.Folder;

public class TableMlpTestsOneIndexByContinent implements Serializable {

	private static final long serialVersionUID = 3261877819408058887L;

	private ArchiveDescription description;
	
	private List<RowOneIndexByContinent> rows = new ArrayList<>();
	
	private String savePath = Folder.ONE_INDEX_BY_CONTINENT_OUTPUT + "TableMlpTestsOneIndexByContinent_";

	public TableMlpTestsOneIndexByContinent (ArchiveDescription description) {
		this.description = description;
	}
	
	public void printTable() {
		
		System.out.println(getHeader());
		
		for (RowOneIndexByContinent row : rows) {
			System.out.println(row);
		}
	}
	
	public void saveTableAsTxt () throws IOException {
		
		PrintWriter writer = new PrintWriter(new FileWriter(new File(savePath + description + ".txt")));
		
		writer.println(getHeader());
		
		for (RowOneIndexByContinent row : rows) {
			writer.println(row);
		}
		
		writer.flush();
		writer.close();
		
	}
	
	public void serialize () {
		
		FileOutputStream arq = null;
		ObjectOutputStream out = null;
		
		try {

			arq = new FileOutputStream(savePath + description + ".ser");
	 
			out = new ObjectOutputStream(arq);
	 
			out.writeObject(this);
			
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				arq.close();
				out.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		
	}
	
	public TableMlpTestsOneIndexByContinent deserialize () {
		
		FileInputStream arqLeitura = null;
		ObjectInputStream in = null;
		
		try {

			arqLeitura = new FileInputStream(savePath + description + ".ser");
			in = new ObjectInputStream(arqLeitura);
			return (TableMlpTestsOneIndexByContinent) in.readObject();
			
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				arqLeitura.close();
				in.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	 
		return null;
		
	}
	
	public List<RowOneIndexByContinent> getRows () {
		return rows;
	}

	private String getHeader() {
		return "bolsas \t #neuronios \t taxaAprendizado \t momento \t #epocas \t acerto(%)";
	}

	public void addRow(String indexes, int hiddenLayers, double learningRate, double momentum, int trainingTime, BigDecimal percentualCorrect) {
		rows.add(new RowOneIndexByContinent(indexes, hiddenLayers, learningRate, momentum, trainingTime, percentualCorrect));
	}

}
